from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime
from app.database import Base
from pytz import timezone

def get_beijing_time():
    beijing_tz = timezone('Asia/Shanghai')
    return datetime.now(beijing_tz)

class Order(Base):
    __tablename__ = "orders"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    spot_id = Column(Integer, ForeignKey("spots.id"))
    quantity = Column(Integer)
    total_price = Column(Float)
    status = Column(String(20))  # pending, paid, completed, cancelled
    visit_date = Column(DateTime)
    contact_name = Column(String(50))  # 添加联系人字段
    contact_phone = Column(String(20))  # 添加联系电话字段
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    expires_at = Column(DateTime, nullable=False)
    rating = Column(Integer, nullable=True)  # 评分 1-5
    comment = Column(String(500), nullable=True)  # 评价内容
    commented_at = Column(DateTime(timezone=True), nullable=True)  # 评价时间
    is_anonymous = Column(Boolean, default=False)  # 是否匿名评价

    # 关联关系
    user = relationship("User", back_populates="orders")
    spot = relationship("Spot", back_populates="orders") 